<template>
  <div class="edit-container">
    <van-nav-bar
      title="编辑页"
      left-text="返回"
      left-arrow
      @click-left="$router.back()"
    />
    <div class="avatar">
      <van-image
        width="120"
        height="120"
        :src="userInfo.photo"
        fit="cover"
        round
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell
      title="名称"
      :value="userInfo.name"
      is-link
      @click="editNameClick"
    />
    <van-cell
      title="性别"
      :value="userInfo.gender ? '女' : '男'"
      is-link
      @click="showPopup = true"
    />
    <van-cell
      title="生日"
      :value="userInfo.birthday"
      is-link
      @click="
        showDatePicker = true;
        currentDate = new Date(userInfo.birthday);
      "
    />
    <van-dialog
      v-model="showDialog"
      title="编辑姓名"
      show-cancel-button
      @confirm="updateName"
    >
      <van-field ref="input" v-model="newName" placeholder="请输入姓名" />
    </van-dialog>
    <!-- 性别修改 -->
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar title="修改性别" left-arrow @click-left="$router.back()" />
      <van-cell value="男" is-link @click="updateGender(0)" />
      <van-cell value="女" is-link @click="updateGender(1)" />
    </van-popup>
    <!-- 修改生日 -->
    <van-popup v-model="showDatePicker" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showDatePicker = false"
        @confirm="updateBirthday"
      />
    </van-popup>
    <!-- 图片裁切盒子 -->
    <!-- 裁切区域弹框 -->
    <div class="cropper" v-show="showCropper">
      <!-- 裁切组件 -->
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="120"
        autoCropHeight="120"
      ></vueCropper>
      <van-button type="primary" @click="showCropper = false">取消</van-button>
      <van-button type="info" @click="updatePhoto">确认</van-button>
    </div>
  </div>
</template>


<script>
import { mapState } from "vuex";
import { editUserInfo, editUserPhoto } from "./user";
import * as moment from "moment";
import { VueCropper } from "vue-cropper";
export default {
  name: "edit",
  components: {
    VueCropper,
  },
  data() {
    return {
      showDialog: false,//编辑姓名弹窗
      newName: "",
      showPopup: false,//编辑性别弹窗
      minDate: new Date(1996, 0, 1),//最小值
      maxDate: new Date(2025, 10, 1),//最大值
      currentDate: new Date(),//当前选中的日期
      showDatePicker: false,//裁切图片窗口
      img: "",//要裁切的图片
      showCropper: false,
    };
  },
  methods: {
    editNameClick() {
      // 弹出编辑姓名弹窗
      this.showDialog = true;
      this.newName = this.userInfo.name;
      this.$nextTick(() => {
        this.$refs.input.focus();
      });
    },
    // 修改名字
    updateName() {
      editUserInfo({ name: this.newName }).then((res) => {
        console.log(res);
        this.$toast.success("修改成功");
        this.$store.commit("updateUserInfo", {
          ...this.userInfo,
          name: this.newName,
        });
      });
    },
    // 修改性别
    updateGender(gender) {
      editUserInfo({ gender }).then((res) => {
        console.log(res);
        // 弹窗提示
        this.$toast.success("修改成功");
        // 更新vuex数据
        this.$store.commit("updateUserInfo", {
          ...this.userInfo,//线传入原始数据
          gender,//然后使用新的name覆盖原始的name
        });
        // 关闭弹窗 popup弹窗默认是点击空白区域关闭 我们点击的是cell需要手动关闭
        this.showPopup = false;
      });
    },
    // 修改生日
    updateBirthday() {
      // 转换日期格式
      let birthday = moment(this.currentDate).format("YYYY-MM-DD");
      console.log(birthday);
      editUserInfo({ birthday }).then((res) => {
        
        this.showDatePicker = false;
        // 弹窗提示
        this.$toast.success("更新成功");
        // 更新vuex数据
        this.$store.commit("updateUserInfo", {
          ...this.userInfo,
          birthday,
        });
      });
    },
    // 上传图像
    afterRead(file) {
      // 使用formdata处理文件数据
      console.log(file.file);
      // 设置需要裁切图片交给cropper裁切 这里需要使用base64编码图片
      this.img = file.content;
      // 弹出裁切框
      this.showCropper = true;
    },
    // 裁切确认按钮 裁切之后上传图片
    updatePhoto() {
      // 获取Blob这种格式
      // formData + 接口提交到远程
      this.$refs.cropper.getCropBlob((data) => {
        let fd = new FormData();
        fd.append("photo", data);
        // 发送请求
        editUserPhoto(fd).then((res) => {
          console.log(res);
          // 弹窗提示
          this.$toast.success("更新成功");
          // 更新vuex数据
          this.showCropper = false;
          this.$store.commit("updateUserInfo", {
            ...this.userInfo,
            photo: res.data.data.photo,
          });
        });
      });
    },
  },
  // 注册组件
  computed: {
    ...mapState(["userInfo"]),
  },
};
</script>


<style lang="less">
.edit-container {
  .van-nav-bar__content {
    background-color: #009afb;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-nav-bar__text {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  .avatar {
    display: flex;
    height: 150px;
    justify-content: center;
    align-items: center;
  }
  .van-uploader {
    position: absolute;
    opacity: 0;
    width: 120px;
    height: 120px;
    .van-uploader__upload {
      width: 120px;
      height: 120px;
    }
  }
  .cropper {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    .van-button--primary {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .van-button--info {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
}
</style>